package top.sjydzq.www.controller.Management;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import top.sjydzq.www.bean.Admin;
import top.sjydzq.www.service.UserService;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/management/")
public class MLoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login() {
        return "Management/Login/index";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String handleLogin(String name, String password, HttpSession session) {
        Admin admin = userService.selectByName(name);

        boolean status;

        if (admin != null && admin.getApwd().equals(password)) {
            status = true;

            session.setAttribute("admin", admin);
        } else {
            status = false;
        }

        if (status) {
            return "redirect:/management/index";
        } else {
            return "redirect:/management/login";
        }
    }

    @RequestMapping("logout")
    public String handleLogout(HttpSession session) {
        session.removeAttribute("admin");

        return "redirect:/management/login";
    }
}
